#ifndef CLIENT_SESSION_H
#define CLIENT_SESSION_H

#include <boost/asio.hpp>
#include <memory>
#include <queue>
#include <google/protobuf/message.h>
#include <proto/generated/game_protocol.pb.h>

using boost::asio::ip::tcp;


class GameServer;

class ClientSession : public std::enable_shared_from_this<ClientSession> {
public:
	ClientSession(tcp::socket socket, GameServer& server);
	~ClientSession();

	void start();
	void deliver(const mcseerserver::ServerMessage& msg);
	int get_id() const { return id_; }
	tcp::socket& socket() { return socket_; }
	std::string get_player_name() const { return player_name_; }

private:
	void read_header();
	void read_body();
	void write();
	void handle_message(const mcseerserver::ClientMessage& msg);
	void handle_login(const mcseerserver::LoginRequest& login);
	void handle_move(const mcseerserver::MoveRequest& move);
	void handle_chat(const mcseerserver::ChatMessage& chat);

	tcp::socket socket_;
	GameServer& server_;
	int id_;
	uint32_t body_length_;
	std::string player_name_;

	enum {max_length = 1024};
	char read_data_[max_length];
	std::vector<char> write_buffer_;

	std::queue<std::vector<char>> write_msgs_;

};

#endif
